package com.payment.common.utils;

import java.io.IOException;
import java.util.HashMap;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;


/**
 * @Description: 环信获取Token
 * @author: zhangxiaohao
 * @date: 2019/6/27 19:39
 */
public class TokenUtils {
    public static String GRANT_TYPE = PropertiesUtil.getProperty("GRANT_TYPE");
    private static String CLIENT_ID = PropertiesUtil.getProperty("CLIENT_ID");
    private static String CLIENT_SECRET = PropertiesUtil.getProperty("CLIENT_SECRET");
    private static String ORG_NAME = PropertiesUtil.getProperty("ORG_NAME");
    private static String APP_NAME = PropertiesUtil.getProperty("APP_NAME");
    private static String LINKED_URL = PropertiesUtil.getProperty("LINKED_URL");
    private static String ACCESS_TOKEN;

    public static HashMap<String, String> doPost(String url, JSONObject json, String token) {
        HttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(url);
        if (token != null) {
            post.addHeader("Authorization", "Bearer " + token);
        }
        String code = "";
        JSONObject response = null;
        HashMap<String, String> args = new HashMap<>();
        try {
            if (json != null) {
                StringEntity s = new StringEntity(json.toString());
                s.setContentEncoding("UTF-8");
                s.setContentType("application/json");
                post.setEntity(s);
            }
            HttpResponse res = client.execute(post);
            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                String result = EntityUtils.toString(res.getEntity());
                response = JSONObject.parseObject(result);
            }
            code = res.getStatusLine().getStatusCode() + "";
        } catch (Exception e) {
            //throw new RuntimeException(e);
            args.put("code", code);
            args.put("response", null);
            return args;
        }

        args.put("code", code);
        if ("200".equals(code)) {
            if (response != null) {
                args.put("response", response.toString());
            } else {
                args.put("response", null);
            }
        } else {
            args.put("response", null);
        }
        return args;
    }

    public static String getAccessToken() {
        String url = LINKED_URL + ORG_NAME + "/" + APP_NAME + "/token";
        String message = "{'grant_type':'" + GRANT_TYPE + "','client_id': '" + CLIENT_ID + "','client_secret': '" + CLIENT_SECRET + "'}";
        HashMap<String, String> result = TokenUtils.doPost(url, JSONObject.parseObject(message), null);
        JSONObject response = JSONObject.parseObject(result.get("response").toString());
        ACCESS_TOKEN = "" + response.get("access_token");
        return ACCESS_TOKEN;
    }

    public static HashMap<String, String> getUserName(String username) {
        String url = LINKED_URL + ORG_NAME + "/" + APP_NAME + "/users/" + username;
        String token = getAccessToken();
        HashMap<String, String> result= null;
        if (token != null) {
            result = TokenUtils.doPost(url, null, token);
        }
        return result;
    }

    public static String getAccessUsers(String username, String password) {
        String url = LINKED_URL + ORG_NAME + "/" + APP_NAME + "/users";
        String message = "{'username':'" + username + "','password':'" + password + "','nickname':'" + username + "'}";
        String token = getAccessToken();
        HashMap<String, String> result = TokenUtils.doPost(url, JSONObject.parseObject(message), token);
        String response=result.get("response");
        return response;
    }

    public static void main(String[] args) throws IOException {
        //System.out.println(getAccessToken());
        /*String username=("aaaa123456" + new Random().nextInt(500));
          String password="123456";
          System.out.println(getAccessUsers(username,password));*/
        //String aa = "asdadasdasdaaaaaaaadasdasdsa";
        //String password = "123456";
        //System.out.println(aa.length());
       /*for (int i=5;i< aa.length(); i++){
          String username=("aabb1234" + new Random().nextInt(500));
          System.out.println(getAccessUsers(username,password));
          System.out.println(i+"-----------");
        }*/
        //String username = "A123456";
        //System.out.println(getAccessUsers(username,password));
        //System.out.println(getUserName(username));
    }


}
